package com.example.cv.controller;

import com.example.cv.model.Candidate;
import com.example.cv.model.Employee;
import com.example.cv.model.Employer;
import com.example.cv.model.Position;
import com.example.cv.service.EmployeeService;
import com.example.cv.service.EmployerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周杰
 */
@Controller
public class EmployerController {
    public EmployerService employerService;

    @Autowired
    public void setEmployerService(EmployerService employerService) {
        this.employerService = employerService;
    }

    public EmployeeService employeeService;

    @Autowired
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /**
     * description:用人单位登陆的post形式
     * @param employer,session,model
     * @return java.lang.String
     */
    @PostMapping("/employerLogin")
    public String employerLogin(Employer employer, HttpSession session, Model model) {
        Employer e = employerService.findEmployer(employer);
        if (e != null) {
            session.setAttribute("employer", e);
            return "redirect:employerRecommend?e_id="+e.getE_id();
        } else {
            model.addAttribute("msg","用户名或者密码错误");
            return "login";
        }
    }
    /**
     * description:用人单位退出
     * @param
     * @return java.lang.String
     */
    @GetMapping("/employerExit")
    public String exitEmployer(HttpSession session) {
        session.removeAttribute("employer");
        return "redirect:login";
    }
    /**
     * description:用人单位注册入口（get）
     * @param
     * @return java.lang.String
     */
    @GetMapping("/employerRegister")
    public String toRegister() {
        return "employerRegister";
    }
    /**
     * description:求职者注册提交（post）
     * @param
     * @return java.lang.String
     */
    @PostMapping("/employerRegister")
    public String toRegister(Employer employer,HttpSession session, Model model) {
        boolean ret = employerService.addEmployer(employer);
        return employerLogin(employer,session,model);
    }

    /**
     * description:判断显示的求职者是否是候选者
     * @param e_id,model
     * @return java.lang.String
     */
    public void cIsEmployee(int e_id,Model model){
        List<Employee>employees=employeeService.findEmployeesByEId(e_id);
        Map<Integer, Employee>cIsEmployee = new HashMap<Integer, Employee>();
        for (int i = 0; i < employees.size(); i++) {
            cIsEmployee.put(employees.get(i).getC_id(),employees.get(i));
        }
        model.addAttribute("cIsEmployee",cIsEmployee);
    }

    /**
     * description:查询所有求职者
     * @param session
     * @return String
     */
    @GetMapping("/findAllCandidates")
    public String findAllCandidates(int e_id, HttpSession session, Model  model, @RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "9") int pageSize){
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Candidate> list=employerService.findAllCandidates();
        //放入分页对象中,第二个参数表示最多连续显示的页数5
        PageInfo<Candidate> pageInfo=new PageInfo<Candidate>(list,5);
        session.setAttribute("candidates",list);
        cIsEmployee(e_id,model);
        model.addAttribute("pageInfo",pageInfo);
        return "employerSearch";
    }
    /**
     * description:用人单位根据用人喜好动态查询求职者
     * @param candidate
     * @return String
     */
    @PostMapping("/findCandidatesByParams")
    public String findCandidatesByParams(int e_id,Candidate candidate, Model model, @RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "9") int pageSize){
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Candidate> list=employerService.findCandidatesByParams(candidate);
        //放入分页对象中,第二个参数表示最多连续显示的页数5
        PageInfo<Candidate> pageInfo=new PageInfo<Candidate>(list,5);
        model.addAttribute("candidates",list);
        cIsEmployee(e_id,model);
        model.addAttribute("pageInfo",pageInfo);
        return "employerSearch";
    }
    /**
     * description:用人单位发布信息
     * @param p_id,model
     * @return java.lang.String
     */
    @GetMapping("/publishPosition")
    public String publishPosition(int p_id,int e_id,Model model){
        Position position =employerService.findPositionByPId(p_id);
        if(position.getRelease_time()==null){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String transformDate=simpleDateFormat.format(new Date());
            position.setRelease_time(transformDate);
            boolean flag=employerService.updatePosReleaseTime(position);
            if(flag)  model.addAttribute("message","发布成功！");
            else  model.addAttribute("message","发布失败！");
        }else{
            model.addAttribute("message","您已发布过招聘信息！不能重复发布！");
        }
        return findAllPositionsByEId(e_id,model,1,9);
    }
    /**
     * description:用人单位撤销发布信息
     * @param e_id,model
     * @return java.lang.String
     */
    @GetMapping("/cancelPosition")
    public String cancelPosition(int p_id,int e_id,Model model){
        Position position =employerService.findPositionByPId(p_id);
        if(position.getRelease_time()!=null){
            position.setRelease_time(null);
            boolean flag=employerService.deletePosReleaseTime(position);
            if(flag)  model.addAttribute("message","撤销成功！");
            else  model.addAttribute("message","撤销失败！");
        }else{
            model.addAttribute("message","您还未公布招聘信息！");
        }
        return findAllPositionsByEId(e_id,model,1,9);
    }

//    public void updateEmpGet(int e_id,Model model){
//        Employer employer=employerService.findEmployerById(e_id);
//        model.addAttribute("employer",employer);
//    }
//
//    /**
//     * description:用人单位更新企业信息（get）
//     * @param
//     * @return java.lang.String
//     */
//    @GetMapping("/updateEmployer")
//    public String updateEmployer(int e_id,Model model){
//        updateEmpGet(e_id,model);
//        return "employerUpdate";
//    }
//
//    public void updateEmpPost(Employer employer,Model model){
//        boolean flag=employerService.updateEmployer(employer);
//        if(flag){
//            model.addAttribute("modifyMessage","修改成功！");
//        }else{
//            model.addAttribute("modifyMessage","修改失败！");
//        }
//    }

//    /**
//     * description:用人单位更新企业信息（post）
//     * @param
//     * @return java.lang.String
//     */
//    @PostMapping("/updateEmployer")
//    public String updateEmployer(Employer employer,Model model){
//        updateEmpPost(employer,model);
//        return "employerUpdate";
//    }
    /**
     * description:求职者查看用人单位具体信息
     * @param e_id,model
     * @return java.lang.String
     */
    @GetMapping("/employerInfo")
    public String employerInfo(int e_id,Model model){
        Employer employer=employerService.findEmployerById(e_id);
        model.addAttribute("employer",employer);
        return "employerInfo";
    }

    @GetMapping("/employerRecommend")
    public String employerRecommend(int e_id,HttpSession session,Model model, @RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "9") int pageSize){
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Candidate>candidates=employerService.findRecommendCandidates(e_id);
        //放入分页对象中,第二个参数表示最多连续显示的页数5
        PageInfo<Candidate> pageInfo=new PageInfo<Candidate>(candidates,5);
        session.setAttribute("recommendCandidates",candidates);
        cIsEmployee(e_id,model);
        model.addAttribute("pageInfo",pageInfo);
        return "employerRecommend";
    }

    @GetMapping("/findAllPositionsByEId")
    public String findAllPositionsByEId(int e_id,Model model,@RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "9") int pageSize){
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Position>positions=employerService.findAllPositionsByEId(e_id);
        //放入分页对象中,第二个参数表示最多连续显示的页数5
        PageInfo<Position> pageInfo=new PageInfo<Position>(positions,5);
        model.addAttribute("pageInfo",pageInfo);
        return "positionManage";
    }

    @PostMapping("/findPositionsByName")
    public String findPositionsByName(int e_id,String pos_name,Model model,@RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "9") int pageSize){
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Position>positions=employerService.findPositionsByName(e_id,pos_name);
        //放入分页对象中,第二个参数表示最多连续显示的页数5
        PageInfo<Position> pageInfo=new PageInfo<Position>(positions,5);
        model.addAttribute("pageInfo",pageInfo);
        return "positionManage";
    }

    @GetMapping("/addPosition")
    public String addPosition(){
        return "positionAdd";
    }

    @PostMapping("/addPosition")
    public String addPosition(Position position){
        boolean flag=employerService.addPosition(position);
        return "redirect:findAllPositionsByEId?e_id="+position.getE_id();
    }

    @GetMapping("/updatePosition")
    public String updatePosition(int p_id,Model model){
        Position position=employerService.findPositionByPId(p_id);
        model.addAttribute("position",position);
        return "positionUpdate";
    }

    @PostMapping("/updatePosition")
    public String updatePosition(Position position){
        boolean flag=employerService.updatePosition(position);
        return "redirect:findAllPositionsByEId?e_id="+position.getE_id();
    }

    @GetMapping("deletePosition")
    public String deletePosition(int p_id,int e_id){
        boolean flag=employerService.deletePosition(p_id);
        return "redirect:findAllPositionsByEId?e_id="+e_id;
    }
}
